package com.ly.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.entity.TeamType;
import com.ly.pojo.dto.TeamTypeDTO;
import com.ly.service.TeamTypeService;
import com.ly.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author : 黑渊白花
 * @ClassName TeamTypeController
 * @date : 2025/2/9 22:13
 * @Description
 */
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/teamType")
@Slf4j
public class TeamTypeController {
    @Autowired
    private TeamTypeService teamTypeService;

    @GetMapping("/list")
    public Result list(){
        List<TeamType> teamTypes = teamTypeService.list();
        return Result.success(teamTypes);
    }

    @GetMapping("/getClubTypeCounts")
    public Result getClubTypeCounts(){
        List<Map<String, Object>> clubTypeCounts = teamTypeService.getClubTypeCounts();
        return Result.success(clubTypeCounts);
    }

    @GetMapping("/page")
    public Object page( TeamTypeDTO teamTypeDTO){
        log.info("{}",teamTypeDTO);
        IPage<TeamType> page = teamTypeService.getPage(teamTypeDTO);
        return page;
    }


    @PostMapping("/save")
    public Result save(@RequestBody TeamType teamType){
        teamTypeService.save(teamType);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody TeamType teamType){
        teamTypeService.updateById(teamType);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        TeamType teamType = teamTypeService.getById(id);
        return Result.success(teamType);
    }

    //判断是否有关联社团，如果有则无法移除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        Boolean isDelete = teamTypeService.delete(id);
        if(isDelete){
            return Result.success("删除成功");
        }
        return Result.warn("存在关联社团，无法移除");
    }

}
